package joymouse.logger;

import java.io.PrintStream;

/**
 * The base class for all loggers which also stores the current logger.
 *
 * @author Ben Wolsieffer
 */
public abstract class Logger {

    /**
     * The current {@code Logger}.
     */
    private static Logger instance = new SystemIOLogger();

    /**
     * A class used to wrap a simple {@link String} message in a
     * {@link Loggable}.
     */
    private static class Info implements Loggable {

        /**
         * Prints the message the specified {@link PrintStream}.
         *
         * @param stream the stream to print to
         */
        @Override
        public void log(PrintStream stream) {
            stream.println(message);
        }
        private String message;

        /**
         * Creates a {@code Info} object that wraps the specified {@link String}
         *
         * @param message the message
         */
        private Info(String message) {
            this.message = message;
        }

        /**
         * {@inheritDoc} {@code Info}s are log level 0.
         */
        @Override
        public int getLevel() {
            return 0;
        }
    }

    /**
     * Logs the specified {@link Loggable} to this {@code Logger}. This method
     * is overridden to define how the logger logs.
     *
     * @param msg
     */
    public abstract void log(Loggable msg);

    /**
     * Logs the specified message to the {@code Logger}. This message is wrapped
     * in a {@link Info} object.
     *
     * @param msg the message to log
     */
    public final void log(String msg) {
        log(new Info(msg));
    }

    /**
     * Gets the current {@code Logger}. This logger is currently hard coded.
     *
     * @return the current {@code Logger}
     */
    public static Logger getLogger() {
        return instance;
    }
}
